package com.xiang.sql.service;

import java.util.ArrayList;
import java.util.List;

import jakarta.annotation.Resource;

import org.springframework.stereotype.Service;

import com.alibaba.fastjson2.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xiang.api.Util;
import com.xiang.sql.entity.User;
import com.xiang.sql.StaticSql;
import com.xiang.sql.entity.Group;
import com.xiang.sql.entity.Prize;
import com.xiang.sql.entity.PrizeGroup;
import com.xiang.sql.mapper.PrizeGroupMapper;

/**
 * 奖品链接 服务实现类
 * 
 * @author mac-xiang
 */
@Service
public class PrizeGroupService extends ServiceImpl<PrizeGroupMapper, PrizeGroup> {
  @Resource
  PrizeGroupMapper mapper;

  private QueryWrapper<PrizeGroup> readWrapper(JSONObject p, User per) {
    QueryWrapper<PrizeGroup> ret = Util.getQuery(p, PrizeGroup.class);
    if (per != null && !per.per("奖品链接查询")) {
      ret.eq("`user`", per.id);
      if (per.gid != null && per.gid.length() > 0) {
        ret.or().eq("`group`", per.gid);
      }
    }
    return ret;
  }

  private List<PrizeGroup> getList(JSONObject p, User per) {
    return mapper.selectList(readWrapper(p, per));
  }

  private Page<PrizeGroup> getPage(Page<PrizeGroup> page, JSONObject p, User per) {
    return mapper.selectPage(page == null ? new Page<PrizeGroup>(1, 100) : page, readWrapper(p, per));
  }

  public Object create(JSONObject p, User per) {
    if (per != null && per.per("奖品链接创建")) {
      PrizeGroup entity = Util.getEntity(p, PrizeGroup.class), t;
      entity.createTime = null;
      entity.useTime = 0L;
      entity.cashTime = 0L;
      entity.create = per.id;
      entity.user = null;
      entity.group = null;
      if (entity.content == null) {
        return null;
      }
      if (Util.arrFindIndex(entity.content, entity.prize) < 0) {
        System.out.println("出现错误:" + entity.prize);
        entity.prize = "";
      }

      if (p.containsKey("num")) {
        int l = p.getIntValue("num");
        List<PrizeGroup> list = new ArrayList<>();
        List<String> ret = new ArrayList<>();
        while (l-- > 0) {
          t = entity.clone(Util.sGetId32());
          list.add(t);
          ret.add(t.id);
        }
        this.saveBatch(list);
        return ret;
      }
      mapper.insert(entity);
      return entity;
    }
    throw new RuntimeException("没有权限");
  }

  public int update(JSONObject p, User per) {
    if (per != null && per.per("奖品链接修改")) {
      PrizeGroup entity = Util.getEntity(p, PrizeGroup.class);
      QueryWrapper<PrizeGroup> qw = Util.getQuery(p, PrizeGroup.class);
      if (this.count(qw) > 10) {
        throw new RuntimeException("单次修改数量超过10条");
      }
      // entity.create = null;
      return mapper.update(entity, qw);
    }
    throw new RuntimeException("没有权限");
  }

  public Object read(JSONObject p, User per) {
    Page<PrizeGroup> page = Util.getPage(p, PrizeGroup.class);
    return page == null ? getList(p, per) : getPage(page, p, per);
  }

  public int delete(JSONObject p, User per) {
    if (per != null && per.per("奖品链接删除")) {
      QueryWrapper<PrizeGroup> q = Util.getQuery(p, PrizeGroup.class);
      if (this.count(q) > 10) {
        throw new RuntimeException("删除数据的数量不能超过10条");
      }
      return mapper.delete(q);
    }
    throw new RuntimeException("没有权限");
  }

  public long count(JSONObject p, User per) {
    return this.count(readWrapper(p, per));
  }

  public List<String> ids(JSONObject p, User per) {
    List<String> ret = new ArrayList<>();
    QueryWrapper<PrizeGroup> qw = readWrapper(p, per).select("id");
    mapper.selectList(qw).forEach(e -> {
      ret.add(e.id);
    });
    return ret;
  }

  public String cash(JSONObject p, User per) {
    String ret = "您没有权限兑付";
    Group group = StaticSql.service.group.getById(per.gid);
    if (group != null) {
      PrizeGroup prizeGroup = this.getById(p.getString("id"));
      if (prizeGroup != null) {
        Prize prize = StaticSql.service.prize.getById(prizeGroup.prize);
        if (prize == null) {
          ret = "兑奖已失效";
        } else {
          if (prizeGroup.cashTime.equals(0L)) {
            mapper.update(new UpdateWrapper<PrizeGroup>().eq("id", prizeGroup.id)
                .set("`cash_time`", Util.time()).set("`group`", per.gid));
            ret = String.format("请兑付奖品【%s】", prize.name);
          } else {
            group = StaticSql.service.group.getById(prizeGroup.group);
            ret = String.format("奖品【%s】已于[%s] 由 [%s] 兑付", prize.name, Util.l2times(prizeGroup.cashTime), group.name);
          }
        }
      }
    }
    return ret;
  }
}
